/**
 * 
 */
package com.unicomcz;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.CallableStatement;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.unicomcz.login.UserInfo;
import org.apache.log4j.Logger;

import com.gmku.office.db.Ora2Xls;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author gmku
 *
 */
public class ActionBase extends ActionSupport
{
    public Logger log;
    protected UserInfo   userInfo;
    protected Connection conn = null;
    protected PreparedStatement pstmt = null;
    protected CallableStatement cstmt = null;
    protected String fileName;
    
    public String getFileName()
    {
        return fileName;
    }
    public void setFileName(String fileName)
    {
        this.fileName = fileName;
    }
    public boolean setupConn(String proxoolName) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn= DriverManager.getConnection("proxool."+proxoolName);
        }catch(Exception e) {
            return false;
        }
        return true;
    }
    public String execute() throws Exception{
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        
        return executeAction();
    }
    public String executeAction(){
        return SUCCESS;
    }
    public boolean validatePriv() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
         userInfo = (UserInfo)session.getAttribute("USER_INFO");
        if ((userInfo==null)||(!userInfo.isLogin())){
            //没有登陆
            return false;
        }
        return true;
    }
    public void tearDown() {
        try {
            pstmt.close();
        }catch(Exception e) {
            
        }
        try {
            conn.close();
        }catch(Exception e) {
            
        }
        return;
    }
    /**
     * ʹ��ʱҪ�ر�ע�⣬ע�������Ҫ�������ж�����ط�����
     * @param sql
     * @return
     */
    public String[] getSingle(String sql) {
        ResultSet rs = null;
        String[] rtv = null;
        try {
           pstmt = conn.prepareStatement(sql);
           regParam2single();
           rs = pstmt.executeQuery();
           ResultSetMetaData rsmeta = rs.getMetaData();
           if (!rs.next()) {
               return null;
           }
           rtv = new String[rsmeta.getColumnCount()];
           for (int i=0;i<rsmeta.getColumnCount();i++) {
               rtv[i]=rs.getString(i+1);
           }
           
        }catch(Exception e) {
            log.debug(e);
            return null;
        }
        try {
            rs.close();
        }catch(Exception e) {
            
        }
        try {
            pstmt.close();
        }catch(Exception e) {
            
        }
        return rtv;
    }
    /**
     * 
     * @param sql
     * @return 影响的行数
     */
    public int updateSingle(String sql) {
        int rtv = 0;
        try {
            pstmt = conn.prepareStatement(sql);
            regParam2single();
            rtv = pstmt.executeUpdate();
            conn.commit();
        }catch(Exception e) {
            log.debug(e);
            return -1;
        }
        return rtv;
    }
    public int execProc(String sql) {
        int rtv = 0;
        try {
            cstmt = conn.prepareCall(sql);
            regProcParam();
            rtv = cstmt.executeUpdate();
        }catch(Exception e) {
            log.debug(e);
            return -1;
        }
        return rtv;
    }
    
    public int execProc(String sql,int rtvIndex) {
        int rtv = 0;
        try {
            cstmt = conn.prepareCall(sql);
            regProcParam();
            cstmt.registerOutParameter(rtvIndex, java.sql.Types.INTEGER);
            cstmt.executeUpdate();
            rtv = cstmt.getInt(rtvIndex);
        }catch(Exception e) {
            log.debug(e);
            return -1;
        }
        return rtv;
    }
    
    protected void regProcParam() {
        
    }
    protected void regParam2single() {
        
    }
    protected void regQueryParam() throws Exception{
        
    }
    protected void rs2List(ResultSet rs,List out) throws Exception{
        
    }
    protected void addTotal(List out) throws Exception{
        
    }
    protected boolean query(String connStr,List out,String sql) {
        boolean dbFlag = setupConn(connStr);
        if (!dbFlag) {
            log.debug(userInfo.getUserId()+"登陆系统失败!因为建立数据库连接失败");
            return false;
        }
        try {
            pstmt = conn.prepareStatement(sql);
            log.debug("运行语名"+sql);
            regQueryParam();
            log.debug("注册语句成功");
            ResultSet rs=pstmt.executeQuery();
            log.debug("填写列");
            while (rs.next()) {
                rs2List(rs,out);
            }
            addTotal(out);
            rs.close();
        }catch(Exception e) {
            log.debug(e);
            return false;
        }finally {
            try {
                pstmt.close();
            }catch(Exception e) {
                
            }
            tearDown();
        }
        return true;
    }
    
    public String getConnStr() {
        return "dls";
    }
    public String getSqlStr() {
        return "";
    }
    public Object[] getParams() {
        return new Object[]{};
    }
    /**
     * 注意，如果要使用该方法，要重载2个方法
     * getConnStr getParams,getSqlStr()
     * 
     * 注意要配置fail的跳转
     * @return
     */
    public String xls() {
        log = Logger.getLogger(this.getClass());
        if (!validatePriv()) {
            //校验登陆错误
            return "login";
        }
        boolean dbFlag = setupConn(getConnStr());
        if (!dbFlag) {
            log.debug(userInfo.getUserId()+"导出数据!因为建立数据库连接失败");
            return "fail";
        }
        try { 
            Ora2Xls ora = new Ora2Xls();
            SimpleDateFormat sf = new SimpleDateFormat("HHmmss");
            String xlsfileName = userInfo.getUserId()+
                    sf.format(new java.util.Date())+".xls";
            fileName="/export/"+xlsfileName;
            String projectRealPath= ServletActionContext.getServletContext().getRealPath("/");
            xlsfileName = projectRealPath+"export"+File.separator+xlsfileName;
            log.debug(fileName);
            log.debug(xlsfileName);
            ora.export(conn, getSqlStr(), getParams(),"导出数据",xlsfileName );
            
        }catch(Exception e) {
            log.debug(e);
        }
        return "xls";
    }
    
}
